//
//  CollectionViewController.m
//  DaHaiDouBan
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 Wang Dahai. All rights reserved.
//

#import "CollectionViewController.h"
#import "NineCollectionViewCell.h"
#import "AppTool.h"
#import "MovieDetailViewController.h"
#import "Movie.h"
@interface CollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *collectionArray;
@property (nonatomic, retain) UICollectionView *collectView;

@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 170);
   // flowLayout.minimumLineSpacing = 20;
   // flowLayout.minimumInteritemSpacing = 20;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 0, 20);
    self.collectView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectView];
    [_collectView release];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    self.collectView.backgroundColor = [UIColor whiteColor];
    //注册
    [self.collectView registerClass:[NineCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *movieArrayPath = [document stringByAppendingPathComponent:@"active.xml"];
    
    self.collectionArray = [NSKeyedUnarchiver unarchiveObjectWithFile:movieArrayPath];
    if (self.collectionArray.count == 0) {
        
        //数据请求
        NSString *url_str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
        
        //初始化数组
        self.collectionArray = [NSMutableArray array];
        [AppTool getDataWithAFNFromURL:url_str andBlock:^(id result) {
            
            NSLog(@"================%@",result);
            NSMutableDictionary *dic = result;
            NSString *error_code = [dic objectForKey:@"error_code"];
            NSString *reason = [dic objectForKey:@"reason"];
            NSMutableArray *array = [dic objectForKey:@"result"];
            if ([reason isEqualToString:@"success"]) {
                for (NSMutableDictionary *smallDic in array) {
                    Movie *movie = [[Movie alloc] init];
                    
                    [movie setValuesForKeysWithDictionary:smallDic];
                    
                    [self.collectionArray addObject:movie];
                    
                    [movie release];
                    
                }
                
                
            }else{
                
                NSLog(@"数据请求失败");
                
            }
            /************一定要在有数据之后刷新************/
            [self.collectView reloadData];
            
            //对 model 进行归档,存入沙盒
            [NSKeyedArchiver  archiveRootObject:self.collectionArray     toFile:movieArrayPath];
            
           }];
        
     }
    
}
//设置显示 cell 的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionArray.count;
    
}
//设置内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIdetifier = @"cell";
    NineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdetifier forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor greenColor];
    
    cell.movie = [self.collectionArray objectAtIndex:indexPath.row];
    
    return cell;

}
//cell 的点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    MovieDetailViewController *detailVC = [[MovieDetailViewController alloc] init];
    
    Movie *movie = [self.collectionArray objectAtIndex:indexPath.row];
    
    detailVC.movieID = movie.movieId;
    
    
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
